﻿using System;
using Irony.Ast;
using Irony.Parsing;
using LibraryGuard.Interfaces;

namespace LibraryGuard.Internals.AST
{
	/// <summary>
	/// Represents boolean value.
	/// </summary>
	internal class Boolean
		: IValue
	{
		#region Fields
		private readonly bool Value = false;
		#endregion

		#region Constructors
		public Boolean(bool val)
		{
			this.Value = val;
		}
		#endregion
		
		#region IValue Members
		/// <inheritdoc />
		public TypeCode GetBaseType(IDataProvider data)
		{
			return TypeCode.Boolean;
		}

		/// <inheritdoc />
		public bool CanBeConvertedTo(TypeCode type, IDataProvider data)
		{
			return true;
		}

		/// <inheritdoc />
		public object GetValue(TypeCode type, IDataProvider data)
		{
			switch (type)
			{
				case TypeCode.String:
					return this.Value.ToString();

				case TypeCode.Boolean:
					return this.Value;

				case TypeCode.Int32:
					return this.Value ? 1 : 0;
			}
			return null;
		}
		#endregion

		#region Statics
		public static void Create(AstContext context, ParseTreeNode parseNode)
		{
			parseNode.AstNode = new Boolean((bool)parseNode.Token.Value);
		}
		#endregion
	}
}
